/**
 * Copyright(c) TacoTag/Fukila Organization
 * FileName: ResourceDao.java
 * Created by Lance on 2009/4/21
 * Change History 
 * Date          Flag      Owner      Change Comment
 *
 */
package org.fukila.core.business;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import org.fukila.core.entity.FResource;
import org.fukila.core.util.EMFFactory;

/**
 * Purpose:
 * 
 * @author Lance 
 * @date 2009/4/21
 * ------------[ Revision History ]----------
 * $Date$
 * $Log$
 */
public class ResourceDao {
    public ResourceDao() {
        
    }
    
    /**
     * 
     * @param res
     */
    public void saveResource(FResource res) {
        EntityManagerFactory emf = EMFFactory.get();
        EntityManager em = emf.createEntityManager();
        EntityTransaction t = em.getTransaction(); 
        t.begin();
        em.persist(res);
        em.flush();
        t.commit();        
        em.close();
    }
    
    /**
     * 
     * @param id
     * @return
     */
    public FResource find(Long id) {
        EntityManagerFactory emf = EMFFactory.get();
        EntityManager em = emf.createEntityManager();
        Query q = em.createQuery("select r from org.fukila.core.entity.FResource r where r.id = :id");
        q.setParameter("id",id);
        FResource r = (FResource) q.getSingleResult();
        em.close();        
        return r;
    }
    
}
